import random

# 1. 生成101个随机分数值（1到20）
scores = [random.randint(1, 20) for _ in range(101)]
print("第一行：", " ".join(map(str, scores)))

# 2. 构建二叉树结构（假设是完全二叉树，编号从1到101）
# 初始化权重数组
weights = [0] * (101 + 1)  # 索引0不用，索引1到101对应节点


# 计算权重值（后序遍历，从叶子节点开始）
def calculate_weights(node):
    if node > 101:
        return 0  # 超出范围的节点不存在
    left = 2 * node
    right = 2 * node + 1
    left_score = calculate_weights(left) if left <= 101 else 0
    right_score = calculate_weights(right) if right <= 101 else 0

    if left > 101 and right > 101:
        # 叶子节点
        weights[node] = scores[node - 1]  # 因为scores是0-based
    else:
        # 非叶子节点
        left_val = left_score if left <= 101 else 0  # 左子节点分数值
        right_val = right_score if right <= 101 else 0  # 右子节点分数值
        weights[node] = left_val * right_val + scores[node - 1]

    return scores[node - 1]  # 返回当前节点的分数值


calculate_weights(1)

# 第二行：按编号顺序输出权重值
print("第二行：", " ".join(map(str, weights[1:])))

# 第三行：按权重降序，编号降序排序
nodes = [(weights[i], i) for i in range(1, 102)]
nodes.sort(key=lambda x: (-x[0], -x[1]))
third_line = [str(node[1]) for node in nodes]
print("第三行：", " ".join(third_line))

# 第四行：前序遍历输出权重值
preorder_weights = []


def preorder(node):
    if node > 101:
        return
    preorder_weights.append(str(weights[node]))
    preorder(2 * node)
    preorder(2 * node + 1)


preorder(1)
print("第四行：", " ".join(preorder_weights))
